home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE02 / TPACK / TPACK.ZIP / DEBUG.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-06-01  |  10.3 KB  |  344 lines

  1. {------------------------------------------------------------------------------}
  2. {UNREGISTERED VERSION (6/1/95) PLEASE REDISTRIBUTE IN tPACK.ZIP!
  3.  This revision does not contain everything, nor are the exciting
  4.  DataSetReporter and ExtendedMenu[Item] components included.
  5.  Use SWREG#5906 to receive these, icons and a help file for $130.
  6.  You must register when using this code in a business application!
  7.  You'll receive a license to use this code in up to 50 copies of
  8.  any app you write. In turn you will get responsive e-mail
  9.  tech support and enhancements till I run out of registrations
  10.  or suggestions. Meanwhile.. enjoy the code. Bye! I'll make more.
  11.  {(C)'1995 Michael/Ax-Systems, 71560,1754@Compuserve.com}
  12. {------------------------------------------------------------------------------}
  13.  
  14. unit Debug;
  15.  
  16. {the Debug component has been activate in the demo so you'd find
  17. your way here. this component still contains some earlier code to
  18. route the log to an ini file and or the printer. you should find
  19. these useful where appropriate.
  20. the control flags are set using AdjustDebugFlags.
  21. route text to the trace window using DebugLog}
  22.  
  23. interface
  24.  
  25. uses
  26.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  27.   Forms, Dialogs, StdCtrls, Buttons, Toolbar, MiscComp, ExtCtrls;
  28.  
  29. type
  30.   TDebugDlg = class(TDemoForm)
  31.     Toolbar1: TToolbar;
  32.     ToolButton1: TToolButton;
  33.     Toolbar2: TToolbar;
  34.     Memo1: TMemo;
  35.     procedure ToolButton1Click(Sender: TObject);
  36.   private
  37.     { Private declarations }
  38.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  39.   public
  40.     { Public declarations }
  41.   end;
  42.  
  43.  
  44.   TDebugExtendedComponentOptions = (decEnabled, decDesign
  45.                                   , decCreate, decDestroy, decLoaded, decUpdate
  46.                                   , decInsert, decRemove
  47.                                   , decPrint, decFile, decNotePad );
  48.  
  49.   TDebugExtendedComponentStates =  (decActive,decFormError,decDestroying
  50.                                    ,decPrintSet,decPrinting,decPrintError
  51.                                    ,decFiling,decFileError );
  52.  
  53.   TDebugExtendedComponentFlags = set of TDebugExtendedComponentOptions;
  54.   TDebugExtendedComponentState = set of TDebugExtendedComponentStates;
  55.  
  56.  
  57. {using the flags and log procedure other parts of the app can use debugging services.}
  58.  
  59. procedure DebugLog(Owner:TComponent;const Text:String); export;
  60.  
  61. procedure AdjustDebugFlags(Value:TDebugExtendedComponentFlags); export;
  62.  
  63. {procedure StartNotePad;}
  64.  
  65. const
  66.   DebugFlags:TDebugExtendedComponentFlags = [];
  67.   DebugState:TDebugExtendedComponentState = [];
  68.  
  69. implementation
  70.  
  71. uses
  72.   IniFiles, PasUtils;
  73.  
  74. const
  75.   DebugLogName= '\debuglog.ini';
  76.  
  77. var
  78.   DebugFile: TIniFile;
  79.   DebugPrinter: TextFile;
  80.  
  81. var
  82.   DebugDlg: TDebugDlg;
  83.  
  84. {$R *.DFM}
  85.  
  86. procedure DebugLog(Owner:TComponent;const Text:String);
  87. const
  88.   BufSize=144;
  89.   Count:Longint=0;
  90.   indent:Byte=0;
  91. var
  92.   Buffer:PChar;
  93.   offset:byte;
  94.   txt:string;
  95.  
  96.   procedure tOut(const Text:String);
  97.   begin
  98.     try
  99.       DebugDlg.Memo1.Lines.add(Text);
  100.     except {ignore?}
  101.       end;
  102.   end;
  103.  
  104. begin
  105.  
  106.   if not (decEnabled in DebugFlags) or (decDestroying in DebugState) then
  107.     exit;
  108.  
  109.   if not ((decFormError in DebugState) or (decActive in DebugState)) then
  110.     if not (decFormError in DebugState) then begin
  111.       if DebugDlg=nil then
  112.         DebugDlg:= TDebugDlg.Create(nil)
  113.       else {take our chances on the form really really being there already!}
  114.         ;
  115.       try
  116.         with DebugDlg do begin
  117.           with Memo1.Lines do begin
  118.             Clear;
  119.             Add('Opened '+datetimetostr(now));
  120.             end;
  121.           OnClose:=FormClose;
  122.           Show;
  123.           Update;
  124.           end;
  125.       except
  126.         DebugState:=DebugState+[decFormError];
  127.         raise;
  128.         end;
  129.       DebugState:=DebugState+[decActive]
  130.       end;
  131.  
  132.   if Owner<>nil then
  133.     if csDesigning in Owner.ComponentState then
  134.       if not (decDesign in DebugFlags) then
  135.         exit;
  136.  
  137. {  if (pos('.DCL',paramstr(0))>0) then {do nothing inside library!}
  138. {    if (pos('Create',Text)>0) then
  139.     exit;}
  140.  
  141.   case Text[1] of
  142.   '+',
  143.   '-': offset:=2;
  144.   else
  145.     offset:=1;
  146.   end;
  147.   Count:=Count+1;
  148.   if Text[1] = '-' then
  149.     indent:=indent-2;
  150.  
  151.   txt:=copy(text,offset,255);
  152.   if owner<>nil then
  153.     Txt:=owner.classname+': '+txt;
  154.   tOut(inttostr(Count)+'. '+Spaces(Indent)+txt);
  155.   {}
  156.   if not (decPrintError in DebugState) and (decPrint in DebugFlags) then begin
  157.     if not (decPrinting in DebugState) then
  158.  
  159.       raise
  160.         exception.create('WINPRN must be linked to debug.pas for printing');
  161.  
  162.       {e.g. add 'WINPRN' to the uses clause at the top of the file
  163.        remove/comment out the exception above
  164.        and uncomment the block below.
  165.        WinPrn is originally stored as in \DELPHI\SOURCE\RTL\WIN\WINPRN}
  166.  
  167. {
  168.       try
  169.         AssignDefPrn(DebugPrinter);
  170.         GetMem(Buffer,BufSize);
  171.         TitlePrn(DebugPrinter,StrPCopy(Buffer,'Debugging '+paramstr(0)));
  172.         FreeMem(Buffer,BufSize);
  173.         Rewrite(DebugPrinter);
  174.         DebugState:=DebugState+[decPrinting];
  175.       except on E: Exception do begin
  176.         DebugState:=DebugState+[decPrintError];
  177.         tOut('ERROR printing! '+E.Message);
  178.         end;
  179.         end;
  180. }
  181.     if not (decPrintError in DebugState) then
  182.       writeln(DebugPrinter
  183.        ,inttostr(Count)+'. '+Spaces(Indent)+txt);
  184.     end;
  185.  
  186.   if not (decFileError in DebugState) and (decFile in DebugFlags) then begin
  187.     if not (decFiling in DebugState) then
  188.       try
  189.         DebugFile:=TIniFile.Create(DebugLogName);
  190.         DebugFile.EraseSection(paramstr(0));
  191.         DebugFile.Free;
  192.         DebugState:=DebugState+[decFiling];
  193.       except on E: Exception do begin
  194.         tOut('ERROR erasing section! '+E.Message);
  195.         DebugState:=DebugState+[decFileError];
  196.         end;
  197.         end;
  198.     if (decFiling in DebugState) then
  199.       try
  200.         DebugFile:=TIniFile.Create(DebugLogName);
  201.         DebugFile.WriteString(paramstr(0),IntToStr(Count),'.'+Spaces(Indent)+txt);
  202.         DebugFile.Free;
  203.       except on E: Exception do begin
  204.         tOut('ERROR writing string! '+E.Message);
  205.         DebugState:=DebugState+[decFileError];
  206.         end;
  207.         end;
  208.     end;
  209.   {}
  210.   if Text[1] = '+' then
  211.     indent:=indent+2;
  212.  
  213. end;
  214.  
  215. {}
  216.  
  217. procedure StartNotePad; {could instantiate a shell, but let be simple here.}
  218. const
  219.   BufSize=144;
  220. var
  221.   Buffer:PChar;
  222. begin
  223.   GetMem(Buffer,BufSize);
  224.   WinExec(StrPCopy(Buffer,'Notepad '+DebugLogName),sw_ShowNormal);
  225.   FreeMem(Buffer,BufSize);
  226. end;
  227.  
  228. {}
  229.  
  230. procedure AdjustDebugFlags(Value:TDebugExtendedComponentFlags);
  231. begin
  232.   if not (decPrint in Value) and (decPrint in DebugFlags) then  {print off}
  233.     if (decPrinting in DebugState) then begin
  234.       CloseFile(DebugPrinter);
  235.       DebugState:=DebugState-[decPrinting];
  236.       end;
  237.  
  238.   if not (decFile in Value) and (decFile in DebugFlags) then  {file off}
  239.     if (decFiling in DebugState) then begin
  240.       DebugState:=DebugState-[decFiling];
  241.       if (decNotePad in DebugFlags) then
  242.         StartNotePad;
  243.       end;
  244.  
  245.   if not (decEnabled in Value) and (decEnabled in DebugFlags) then begin{turn all off}
  246.     Value:=Value-[decCreate,decDesign,decDestroy,decLoaded,decUpdate,decInsert,decRemove];
  247.     end;
  248.   if (decEnabled in Value) and not (decEnabled in DebugFlags) then begin{turn all on}
  249.     Value:=Value+[decCreate,decDesign,decDestroy,decLoaded,decUpdate,decInsert,decRemove];
  250.     end;
  251.  
  252.   DebugFlags:=Value;
  253. end;
  254.  
  255.  
  256. {-----------------------------------------------------------------------------------------}
  257. {                                                                                         }
  258. {-----------------------------------------------------------------------------------------}
  259.  
  260. procedure TDebugDlg.FormClose(Sender: TObject; var Action: TCloseAction);
  261. begin
  262.   Action:=caFree;
  263.   DebugDlg:=nil;
  264.   {DebugState:=DebugState-[decActive];}
  265.   DebugState:= [];
  266. end;
  267.  
  268. procedure TDebugDlg.ToolButton1Click(Sender: TObject);
  269. begin
  270.   Close;
  271. end;
  272.  
  273.  
  274. {-----------------------------------------------------------------------------------------}
  275. { INITIALIZATION AND EXIT PROCEDURES                                                      }
  276. {-----------------------------------------------------------------------------------------}
  277.  
  278. procedure InitializeUnit;
  279. var
  280.   i:integer;
  281.   a:string;
  282. begin
  283.   DebugFlags:= [];
  284.   DebugState:= [];
  285.  { if csDesigning in ComponentState then exit;}
  286.   {process the commandline to set the unit's globals to the desired DEBUG state.}
  287.   for i:=1 to ParamCount do begin
  288.     a:=uppercase(ParamStr(i));
  289.     if copy(a,1,2)='/D' then begin
  290.       DebugFlags:=DebugFlags+[decEnabled];
  291.       if Length(a)=2 then
  292.         DebugFlags:=DebugFlags+[decCreate,decDesign,decDestroy,decLoaded,decUpdate,decInsert,decRemove]
  293.       else begin
  294.         if pos('C',a)>0 then DebugFlags:=DebugFlags+[decCreate];
  295.         if pos('D',a)>0 then DebugFlags:=DebugFlags+[decDesign];
  296.         if pos('L',a)>0 then DebugFlags:=DebugFlags+[decLoaded];
  297.         if pos('U',a)>0 then DebugFlags:=DebugFlags+[decUpdate];
  298.         if pos('I',a)>0 then DebugFlags:=DebugFlags+[decInsert];
  299.         if pos('R',a)>0 then DebugFlags:=DebugFlags+[decRemove];
  300.         if pos('P',a)>0 then DebugFlags:=DebugFlags+[decPrint];
  301.         if pos('F',a)>0 then DebugFlags:=DebugFlags+[decFile];
  302.         if pos('N',a)>0 then DebugFlags:=DebugFlags+[decNotepad];
  303.         end;
  304.       end;
  305.     end;
  306. end;
  307.  
  308. {-----------------------------------------------------------------------------------------}
  309.  
  310. procedure FinalizeUnit;
  311. begin
  312.   if (decPrint in DebugFlags) or (decFile in DebugFlags) then {turn off}
  313.     AdjustDebugFlags([]); {stores back into global}
  314. end;
  315.  
  316. {-----------------------------------------------------------------------------------------}
  317. {-----------------------------------------------------------------------------------------}
  318.  
  319. Const
  320.   Initialized: boolean = False;
  321.   SaveExit: Pointer =nil;                    { Saves the old ExitProc }
  322.  
  323. procedure Finalize; far;
  324. begin
  325.   ExitProc := SaveExit;
  326.   FinalizeUnit;
  327. end;
  328.  
  329. procedure Initialize;
  330. begin
  331.   if not Initialized then begin
  332.     Initialized:=True;
  333.     SaveExit := ExitProc;
  334.     ExitProc := @Finalize;
  335.     InitializeUnit;
  336.     end;
  337. end;
  338.  
  339. initialization
  340.   Initialize;
  341. end.
  342.  
  343.  
  344.